package com.gxuwz.business.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.gxuwz.business.entity.Recipe;
import com.gxuwz.util.Pager;

@Repository("recipeDAO")
public class RecipeDAO extends BaseDaoImpl<RecipeDAO>{

	
	/**
	 * 添加
	 */
	public boolean add(Recipe recipe){
		boolean isSucceed = false;
		try {
			getHibernateTemplate().save(recipe);
			isSucceed = true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return isSucceed;
	}
	
	/**
	 * 分页查询
	 */
	public Pager<Recipe> getByPager(Integer page, Integer limit){
		String hql = "from Recipe where 1 = 1 and status = 0 order by treatId";
		try {
			return super.find(hql, null, null, (page-1)*limit, limit);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 获取所有处方编号
	 */
	public List<String> getAllId(){
		String hql = "select recipeId from Recipe where 1 = 1";
		try {
			return (List<String>) getHibernateTemplate().find(hql);
		} catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * 更新
	 */
	public boolean update(Recipe recipe){
		boolean isSucceed = false;
		try {
			getHibernateTemplate().update(recipe);
			isSucceed = true;
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return isSucceed;
	}
	
	/**
	 * 根据诊编查找
	 */
	@SuppressWarnings("unchecked")
	public List<Recipe> getAllByTreatId(String treatId){
		String hql = "from Recipe where 1 = 1 and treatId = ?";
		try {
			return ((List<Recipe>) getHibernateTemplate().find(hql, treatId));
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return null;
	}
	
	/**
	 * 通过处编查找
	 */
	public Recipe findById(String recipeId){
		String hql = "from Recipe where recipeId = ?";
		try {
			return (Recipe) getHibernateTemplate().find(hql, recipeId).get(0);	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 根据诊编查找对应处编集合
	 */
	public List<String> getByTreatId(String treatId){
		String hql = "select recipeId from Recipe where treatId = ?";
		try {
			List<String> recipeIdList = (List<String>) getHibernateTemplate().find(hql, treatId);
			return recipeIdList;
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return null;
	}
}

